﻿''' <summary>
''' 自動ナンバリングキー列属性
''' </summary>
''' <remarks></remarks>
Public Class AutoKeyAttribute
    Inherits KeyAttribute

    Protected Overrides Sub Initialize(col As ColumnAttribute)
        MyBase.Initialize(col)
        col.InsertColumnCommandCreater = AddressOf Me.CreateInsertColumnCommand
    End Sub

    Private Sub CreateInsertColumnCommand(instance As Object, sqlColumnBuilder As System.Action(Of String), sqlValueBuilder As System.Action(Of String), paramRegister As System.Func(Of Object, Type, String))
        '基底である場合、追加列に含めないようにします
        If TableAttributeReader.Read(Me.Column.Table.ModelType).IsRootBase Then Return

        'それ以外の場合、通常の列と同時処理
        Me.Column.DefaultInsertColumnCreater(instance, sqlColumnBuilder, sqlValueBuilder, paramRegister)
    End Sub

    Public Overrides Function CreateKeyQueryAfterSave(trn As System.Data.IDbTransaction) As System.Data.IDbCommand
        '基底でない場合、自動採番処理はありません
        If TableAttributeReader.Read(Me.Column.Table.ModelType).IsRootBase = False Then Return Nothing

        Dim cmd As IDbCommand = trn.Connection.CreateCommand
        cmd.Transaction = trn
        cmd.CommandText = "SELECT @@IDENTITY"
        Return cmd
    End Function

End Class
